<template>
  <div class="home-container">
    <div class="dashboard">
      <div class="header">
        <h1>学习管理系统</h1>
        <p class="subtitle">欢迎使用，{{ userInfo.name }}</p>
      </div>
      
      <!-- 数据统计卡片 -->
      <div class="stats-cards">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-card class="stat-card">
              <div class="stat-content">
                <i class="el-icon-user stat-icon"></i>
                <div class="stat-info">
                  <div class="stat-number">128</div>
                  <div class="stat-label">用户总数</div>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card class="stat-card">
              <div class="stat-content">
                <i class="el-icon-folder stat-icon"></i>
                <div class="stat-info">
                  <div class="stat-number">24</div>
                  <div class="stat-label">项目总数</div>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card class="stat-card">
              <div class="stat-content">
                <i class="el-icon-tickets stat-icon"></i>
                <div class="stat-info">
                  <div class="stat-number">156</div>
                  <div class="stat-label">任务总数</div>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card class="stat-card">
              <div class="stat-content">
                <i class="el-icon-data-analysis stat-icon"></i>
                <div class="stat-info">
                  <div class="stat-number">85%</div>
                  <div class="stat-label">完成率</div>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
      
      <!-- 用户信息和快捷操作 -->
      <div class="content-row">
        <el-col :span="16">
          <el-card class="user-info-card">
            <template #header>
              <div class="card-header">
                <span>用户信息</span>
              </div>
            </template>
            <div class="user-details">
              <el-row :gutter="20">
                <el-col :span="12">
                  <div class="info-item">
                    <i class="el-icon-user info-icon"></i>
                    <div class="info-text">
                      <span class="info-label">用户名:</span>
                      <span class="info-value">{{ userInfo.username }}</span>
                    </div>
                  </div>
                </el-col>
                <el-col :span="12">
                  <div class="info-item">
                    <i class="el-icon-user info-icon"></i>
                    <div class="info-text">
                      <span class="info-label">姓名:</span>
                      <span class="info-value">{{ userInfo.name }}</span>
                    </div>
                  </div>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="12">
                  <div class="info-item">
                    <i class="el-icon-office-building info-icon"></i>
                    <div class="info-text">
                      <span class="info-label">部门:</span>
                      <span class="info-value">{{ userInfo.department }}</span>
                    </div>
                  </div>
                </el-col>
                <el-col :span="12">
                  <div class="info-item">
                    <i class="el-icon-date info-icon"></i>
                    <div class="info-text">
                      <span class="info-label">注册时间:</span>
                      <span class="info-value">2023-01-15</span>
                    </div>
                  </div>
                </el-col>
              </el-row>
            </div>
          </el-card>
        </el-col>
        
        <el-col :span="8">
          <el-card class="quick-actions-card">
            <template #header>
              <div class="card-header">
                <span>快捷操作</span>
              </div>
            </template>
            <div class="actions-grid">
              <div class="action-item" @click="goToUserManagement">
                <i class="el-icon-user action-icon"></i>
                <span>用户管理</span>
              </div>
              <div class="action-item" @click="goToProjects">
                <i class="el-icon-folder action-icon"></i>
                <span>项目管理</span>
              </div>
              <div class="action-item" @click="goToTasks">
                <i class="el-icon-tickets action-icon"></i>
                <span>任务管理</span>
              </div>
              <div class="action-item" @click="goToCourseManagement">
                <i class="el-icon-video-play action-icon"></i>
                <span>课程管理</span>
              </div>
              <div class="action-item" @click="logout">
                <i class="el-icon-switch-button action-icon"></i>
                <span>退出登录</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </div>
      
      <!-- 近期活动 -->
      <div class="activity-section">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>近期活动</span>
            </div>
          </template>
          <el-timeline>
            <el-timeline-item
              v-for="(activity, index) in activities"
              :key="index"
              :timestamp="activity.timestamp"
              placement="top"
            >
              <el-card>
                <h4>{{ activity.title }}</h4>
                <p>{{ activity.content }}</p>
              </el-card>
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

// 获取用户信息
const userInfo = ref({
  username: '',
  name: '',
  department: ''
})

// 近期活动数据
const activities = ref([
  {
    timestamp: '2023-01-20',
    title: '新项目启动',
    content: '电商平台开发项目已正式启动，预计完成时间2023年12月31日。'
  },
  {
    timestamp: '2023-01-18',
    title: '用户管理更新',
    content: '新增5个用户账户，删除2个过期账户。'
  },
  {
    timestamp: '2023-01-15',
    title: '系统维护完成',
    content: '系统维护已完成，优化了数据库查询性能。'
  },
  {
    timestamp: '2023-01-10',
    title: '安全更新',
    content: '系统已更新至最新安全补丁，提升了系统安全性。'
  }
])

const router = useRouter()

// 页面加载时获取用户信息
onMounted(() => {
  const userInfoStr = localStorage.getItem('userInfo')
  if (userInfoStr) {
    userInfo.value = JSON.parse(userInfoStr)
  }
})

// 跳转到用户管理页面
const goToUserManagement = () => {
  router.push('/users')
}

// 跳转到项目管理页面
const goToProjects = () => {
  router.push('/projects')
}

// 跳转到任务管理页面
const goToTasks = () => {
  ElMessage.info('任务管理功能待开发')
}

// 跳转到课程管理页面
const goToCourseManagement = () => {
  router.push('/courses/manage')
}

// 退出登录
const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  router.push('/login')
  ElMessage.success('已退出登录')
}
</script>

<style scoped>
.home-container {
  padding: 0;
  background-color: #f5f7fa;
  min-height: 100%;
}

.dashboard {
  width: 100%;
  height: 100%;
  margin: 0;
}

.header {
  text-align: center;
  margin-bottom: 30px;
}

.header h1 {
  color: #333;
  margin-bottom: 10px;
  font-size: 28px;
}

.subtitle {
  color: #666;
  font-size: 16px;
}

/* 数据统计卡片 */
.stats-cards {
  margin-bottom: 20px;
}

.stat-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 0;
}

.stat-content {
  display: flex;
  align-items: center;
}

.stat-icon {
  font-size: 32px;
  margin-right: 15px;
  color: #409eff;
}

.stat-info {
  display: flex;
  flex-direction: column;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
}

.stat-label {
  font-size: 14px;
  color: #606266;
  margin-top: 5px;
}

.card-header {
  font-weight: bold;
  color: #303133;
}

/* 右侧信息区域卡片 */
.user-info-card, .quick-actions-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

/* 用户信息 */
.user-details {
  padding: 15px 0;
}

.info-item {
  display: flex;
  align-items: center;
  margin: 12px 0;
}

.info-icon {
  font-size: 18px;
  margin-right: 8px;
  color: #409eff;
}

.info-text {
  display: flex;
  flex-direction: column;
}

.info-label {
  font-size: 13px;
  color: #606266;
  margin-bottom: 2px;
}

.info-value {
  font-size: 15px;
  font-weight: 500;
  color: #303133;
}

.status-normal {
  color: #67c23a;
  font-weight: bold;
}

/* 快捷操作 */
.actions-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 10px 0;
}

.action-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid #ebeef5;
}

.action-item:hover {
  background-color: #ecf5ff;
  border-color: #409eff;
}

.action-icon {
  font-size: 20px;
  margin-bottom: 6px;
  color: #409eff;
}

.action-item span {
  font-size: 13px;
  color: #303133;
}

/* 近期活动 */
.activity-section {
  margin-bottom: 20px;
}

.activity-section :deep(.el-card) {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.activity-section :deep(.el-timeline-item__timestamp) {
  color: #606266;
}

.activity-section :deep(.el-card) {
  border: 1px solid #ebeef5;
}

@media (max-width: 768px) {
  .stats-cards :deep(.el-col) {
    margin-bottom: 15px;
  }
  
  .actions-grid {
    grid-template-columns: 1fr;
  }
}
</style>